package com.fduss.artStore.dao.impl;

import com.fduss.artStore.dao.TypesFrameDao;
import com.fduss.artStore.entity.TypesFrame;
import com.fduss.artStore.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by alex on 28/06/2017.
 */
public class TypesFrameDaoImpl implements TypesFrameDao{

    private JdbcUtil util;

    public TypesFrameDaoImpl() {
        util = new JdbcUtil();
    }

    @Override
    public TypesFrame add(TypesFrame typesFrame) {
        Connection conn = util.getConnection();
        String sql = "INSERT INTO `art`.`typesframes` " +
                "(`FrameID`, `Title`, `Price`, `Color`, `Syle`) VALUES (?, ?, ?, ?, ?)";

        PreparedStatement pst = null;
        boolean flag = false;
        
        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesFrame.getFrameId());
            pst.setString(2, typesFrame.getTitle());
            pst.setBigDecimal(3, typesFrame.getPrice());
            pst.setString(4, typesFrame.getColor());
            pst.setString(5, typesFrame.getSyle());
            pst.executeUpdate();
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
        return flag ? typesFrame : null;
    }

    @Override
    public void delete(int typesFrameId) {
        Connection conn = util.getConnection();
        String sql = "DELETE FROM `art`.`typesframes` WHERE `FrameID` = ?";
        PreparedStatement pst = null;
        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesFrameId);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(null, pst, conn);
        }
    }

    @Override
    public TypesFrame getOne(int typesFrameId) {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`typesframes` WHERE `FrameID` =?";
        PreparedStatement pst = null;
        ResultSet rs = null;
        TypesFrame typesFrame = null;

        try{
            pst = conn.prepareStatement(sql);
            pst.setInt(1, typesFrameId);
            rs = pst.executeQuery();
            if (rs.next()){
                typesFrame = buildTypesFrame(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }
        return typesFrame;
    }

    @Override
    public List<TypesFrame> getAll() {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`typesframes`";
        PreparedStatement pst = null;
        ResultSet rs = null;
        List<TypesFrame> typesFrames = new ArrayList<>();

        try{
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()){
                typesFrames.add(buildTypesFrame(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }
        return typesFrames;
    }

    private TypesFrame buildTypesFrame(ResultSet rs) throws SQLException {
        TypesFrame typesFrame = new TypesFrame();
        typesFrame.setColor(rs.getString("Color"));
        typesFrame.setFrameId(rs.getInt("FrameID"));
        typesFrame.setPrice(rs.getBigDecimal("Price"));
        typesFrame.setSyle(rs.getString("Syle"));
        typesFrame.setTitle(rs.getString("Title"));
        return typesFrame;
    }
}
